<?php

session_start();

include ("../includes/connection.php");
include ("../includes/article.php");

$article = new Article;
if (isset($_SESSION["logged_in"])) {
    if (isset($_GET["id"])) {
        $id = $_GET["id"];

        $query = $pdo->prepare("DELETE FROM articles WHERE article_id= ?");

        $query->bindValue(1, $id);
        $query->execute();

        header("Location: delete.php");
    }

    $articles = $article->fetch_all();

    //display delete page
?>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>CMS</title>
            <link rel="stylesheet" href="../assets/style.css" media="all">
        </head>
        <body>
            <div class="container">
                <a id="logo" href="index.php">CMS</a>

                <br>

            <h4>Select an Article to Delete!</h4>

            <form method="get" action="delete.php">
                <select id="" name="id" onchange="this.form.submit();">
                    <?php foreach ( $articles as $article ) { ?>
                    <option value="<?php echo $article["article_id"] ?>">
                        <?php echo $article["article_title"]; ?>
                    </option>
                    <?php } ?>
                </select>
                
            </form>
            </div>
        </body>
    
<?php
} else {
    header("Localtion: index.php");
}

?>
